﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoachingOrganizer.Database.DataAccess;
using CoachingOrganizer.ClientInfrastructure.ServicesEntities.LocalStorage.Tools.MoodLevel;
using CoachingOrganizer.Toolkit.Events;

namespace CoachingOrganizer.Database.DataAccessLayer.Tools.MoodLevel
{
    public class MoodValueDAL : IMoodValueInfo
    {
        private readonly DbManagerBase _dbManager;
        private MoodValueDA _da;
        private DateTime _time;
        private double _value;

        public DateTime Time
        {
            get { return _time; }
            set { _time = value; }
        }

        public double Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public MoodValueDAL(DbManagerBase dbManager)
        {
            _dbManager = dbManager;
        }

        public MoodValueDA CreateNewDA(MoodLevelDA moodLevel)
        {
            return _da = new MoodValueDA
            {
                MoodLevelId = moodLevel.Id
            };
        }

        public ActionResult Save()
        {
            _da.Time = _time;
            _da.Value = _value;
            return _dbManager.SafeRun(() => _dbManager.Context.InsertOrReplace(_da));
        }

        public bool Load(int id)
        {
            MoodValueDA moodValue = _dbManager.Context.Find<MoodValueDA>(da => da.Id == id);
            if (moodValue == null)
                return false;
            Load(moodValue);
            return true;
        }

        public void Load(MoodValueDA moodValue)
        {
            _da = moodValue;
            _time = _da.Time;
            _value = _da.Value;
        }

        public void Remove()
        {
            if (_da != null)
                _dbManager.Context.Delete<MoodValueDA>(_da.Id);
        }
    }
}